#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "debug.h"
#include "os_api.h"


#define _STACK_SIZE		(1024*3)
static void* _task_stack = NULL;

static OSTaskRef _task_ref = NULL;

static OSATimerRef _task_delete_timer_ref = NULL;

void DeleteTaskByTimer(UINT32 id)
{
    OSA_STATUS status;

    if(_task_ref)
    {
        status = OSATaskDelete(_task_ref);
        ASSERT(status == OS_SUCCESS);
        _task_ref = NULL;
	   
        if(_task_stack)
        {
            free(_task_stack);
            _task_stack = NULL;
        }
    }

    OSATimerDelete (_task_delete_timer_ref);
}

extern void DelayInMilliSecond(unsigned int us);
extern void Timer0_Switch(unsigned char OnOff);

static void _task(void *ptr)
{
	int count = 5;
	while(count--){		
		
		OSATaskSleep(1);		// 5ms
		LOG_PRINTF("=====test1======count %d\n", count);
		OSATaskSleep(1);		// 5ms
		LOG_PRINTF("=====test2======count %d\n", count);
		
		
		Timer0_Switch(1);
		DelayInMilliSecond(1000);	// 1ms
		LOG_PRINTF("=====test3======count %d\n", count);
		DelayInMilliSecond(1000);	// 1ms
		LOG_PRINTF("=====test4======count %d\n", count);
		Timer0_Switch(0);
	}
	
	OSATimerCreate(&_task_delete_timer_ref);
	OSATimerStart(_task_delete_timer_ref, 400, 0, DeleteTaskByTimer, 0);
}


int main(void)
{	
    int ret;

	_task_stack = malloc(_STACK_SIZE);
    ASSERT(_task_stack != NULL);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);
	
	return 0;
}